//
//  WDSliderContenerScrollView.m
//  MyDemos
//
//  Created by iMac on 2018/3/8.
//  Copyright © 2018年 iMac. All rights reserved.
//

#import "WDSliderContenerScrollView.h"


@interface WDSliderContenerScrollView()<UIScrollViewDelegate>

@property (nonatomic, strong) NSArray *childViews;
@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, strong) NSDictionary *viewsDictionary;
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, assign) CGFloat lastContentOffsetX;


@end

@implementation WDSliderContenerScrollView

+ (instancetype)wdSliderContenerScrollViewWithFrame:(CGRect)frame views:(NSArray *)views selectIndex:(NSInteger)index {
    WDSliderContenerScrollView *scroll = [[WDSliderContenerScrollView alloc] initWithFrame:frame];
    scroll.childViews = views;
    scroll.currentIndex = index;
    return scroll;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.pagingEnabled = YES;
        self.bounces = NO;
        self.delegate = self;
    }
    return self;
}

- (void)setChildViews:(NSArray *)childViews {
    _childViews = childViews;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    for (int i = 0; i < childViews.count; i++) {
        NSNumber *key = [NSNumber numberWithInt:i];
        UIView *view = (UIView *)childViews[i];
        dict[key] = view;
    }
    self.viewsDictionary = [dict copy];
}
- (void)setCurrentIndex:(NSInteger)currentIndex {
    _currentIndex = currentIndex;
    // NSLog(@"----index: %ld", currentIndex);
    int dataCount = 0;
    NSMutableArray *dataSource = [NSMutableArray array];
    CGFloat offsetX;
    if (currentIndex == 0) {
        for (int i = 0; i < self.childViews.count; i++) {
            if (dataCount > 2) {
                break;
            }
            NSNumber *key = [NSNumber numberWithInt:i];
            [dataSource addObject:self.viewsDictionary[key]];
            dataCount++;
        }
        offsetX = 0;
    }
    else if (currentIndex == self.childViews.count - 1) {
        for (int i = (int)self.childViews.count - 1; i >= 0; i--) {
            if (dataCount > 2) {
                break;
            }
            dataCount++;
        }
        for (int i= 0; i < dataCount; i++) {
            NSNumber *key = [NSNumber numberWithInt:i];
            // NSLog(@"%@", key);
            [dataSource addObject:self.viewsDictionary[key]];
        }
        offsetX = self.frame.size.width * MAX(0, dataCount-1);
    }
    else {
        [dataSource addObject: [self.viewsDictionary objectForKey:[NSNumber numberWithInteger:currentIndex - 1]]];
        [dataSource addObject: [self.viewsDictionary objectForKey:[NSNumber numberWithInteger:currentIndex]]];
        [dataSource addObject: [self.viewsDictionary objectForKey:[NSNumber numberWithInteger:currentIndex + 1]]];
        dataCount += 3;
        offsetX = self.frame.size.width;
    }
    self.contentSize = CGSizeMake(self.frame.size.width * dataCount, 0);
    self.dataSource = [dataSource copy];
    self.contentOffset = CGPointMake(offsetX, 0);
    self.lastContentOffsetX = self.contentOffset.x;
}
- (void)setDataSource:(NSArray *)dataSource {
    _dataSource = dataSource;
    for (int i = 0; i < dataSource.count; i++) {
        UIView *view = self.dataSource[i];
        view.frame = CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height);
        [self addSubview:view];
    }
}

#pragma mark -scroll
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//    NSLog(@"scrollView.contentOffset.x---------- %f", scrollView.contentOffset.x);
//    NSLog(@"self.lastContentOffsetX---------- %f", self.lastContentOffsetX);
    if (scrollView.contentOffset.x > self.lastContentOffsetX) {
        self.currentIndex++;
    }
    else if (scrollView.contentOffset.x < self.lastContentOffsetX) {
        self.currentIndex--;
    }
    else {
        return;
    }
    if (self.didScroll) {
        self.didScroll(self.currentIndex);
    }
    self.lastContentOffsetX = scrollView.contentOffset.x;
}

- (void)jumpTo:(NSInteger)index {
    NSMutableArray *marr = [NSMutableArray arrayWithArray:self.dataSource];
    CGFloat transctionX = 0;
    if (index > self.currentIndex) {
        [marr removeLastObject];
        [marr addObject:self.viewsDictionary[[NSNumber numberWithInteger:index]]];
        transctionX = self.frame.size.width;
    } else if (index < self.currentIndex) {
        [marr removeObjectAtIndex:0];
        NSArray *temp = [NSArray arrayWithArray:marr];
        marr = [NSMutableArray array];
        [marr addObject: self.viewsDictionary[[NSNumber numberWithInteger:index]]];
        [marr addObjectsFromArray:temp];
        transctionX = -self.frame.size.width;
    }
    self.dataSource = [marr copy];
    [UIView animateWithDuration:0.25 animations:^{
        self.contentOffset = CGPointMake(self.contentOffset.x + transctionX, 0);
    }];
    self.lastContentOffsetX = self.contentOffset.x;
    self.currentIndex = index;
}
@end
